/*** 
This do file produces the population county-level data we use from the CSV downloaded 
from Census.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Create required folders
cap mkdir "${root}/data/derived/ACS 2014-2018 5-Year County"
cap mkdir "${root}/data/derived/ACS 2014-2018 5-Year County/Individual Variables"

*-------------------------------------------------------------------------------
* Load and clean raw data
*-------------------------------------------------------------------------------

cd "${root}/data/derived/ACS 2014-2018 5-Year County/Individual Variables"

project, uses("${root}/data/dvc/ACS 2014-2018 5-Year County/Population/ACSDT5Y2018.B01003_2020-08-21T133240.zip") raw
unzipfile "${root}/data/dvc/ACS 2014-2018 5-Year County/Population/ACSDT5Y2018.B01003_2020-08-21T133240.zip", replace

project, uses("ACSDT5Y2018.B01003_data_with_overlays_2020-08-12T144058.csv")
import delimited "ACSDT5Y2018.B01003_data_with_overlays_2020-08-12T144058.csv", varnames(2) clear

* Remove sections of the id variable other than the county FIPS.
replace id = subinstr(id, "0500000US", "", .)

* Renaming geographic variables for clarity
rename id county_fips
rename geographicareaname county_name
assert strlen(county_fips) == 5

* Keeping only the county variable and the population variable
rename estimatetotal pop_2014_2018_est
keep county_fips pop_2014_2018_est

* Convert all variables to numeric
destring county_fips pop_2014_2018_est, replace

*-------------------------------------------------------------------------------
* Save file
*-------------------------------------------------------------------------------

save "${root}/data/derived/ACS 2014-2018 5-Year County/Individual Variables/ACS 2014-2018 Population.dta", replace
project, creates("${root}/data/derived/ACS 2014-2018 5-Year County/Individual Variables/ACS 2014-2018 Population.dta")
